package com.isoft.mapper;

import com.isoft.entity.DfmeHbttEntity;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Param;

@Mapper
public interface DfmeHbttMapper {
    @Insert("INSERT INTO MSG_META(SENDER, RECEIVER, SEQUENCE_NUMBER, DATE_TIME, MSG_TYPE, SUB_TYPE) VALUES(#{entity.meta.sender}, #{entity.meta.receiver}, #{entity.meta.sequenceNumber}, TO_TIMESTAMP(#{entity.meta.dateTime}, 'YYYYMMDDHH24MISS'), #{entity.meta.type}, #{entity.meta.subType})")
    @Options(useGeneratedKeys = true, keyProperty = "meta.msgId", keyColumn = "MSG_ID")
    void insertMeta(@Param("entity") DfmeHbttEntity entity);

    @Insert("MERGE INTO FLIGHT_BASE USING (SELECT #{dflt.flightId} AS FLIGHT_ID FROM DUAL) S ON (FLIGHT_BASE.FLIGHT_ID = S.FLIGHT_ID) " +
            "WHEN MATCHED THEN UPDATE SET " +
            "FLIGHT_FILE_ID = #{dflt.flightFileId,jdbcType=VARCHAR}, " +
            "FLIGHT_IDENTIFIER = #{dflt.flightIdentifier,jdbcType=VARCHAR}, " +
            "HBID = #{dflt.hbid,jdbcType=VARCHAR}, " +
            "NFLN = #{dflt.nfln,jdbcType=VARCHAR}, " +
            "NAWC = #{dflt.nawc,jdbcType=VARCHAR} " +
            "WHEN NOT MATCHED THEN INSERT (" +
            "FLIGHT_ID, FLIGHT_FILE_ID, FLIGHT_IDENTIFIER, HBID, NFLN, NAWC) " +
            "VALUES (" +
            "#{dflt.flightId,jdbcType=NUMERIC}, " +
            "#{dflt.flightFileId,jdbcType=VARCHAR}, " +
            "#{dflt.flightIdentifier,jdbcType=VARCHAR}, " +
            "#{dflt.hbid,jdbcType=VARCHAR}, " +
            "#{dflt.nfln,jdbcType=VARCHAR}, " +
            "#{dflt.nawc,jdbcType=VARCHAR})")
    int insertFlightBase(DfmeHbttEntity entity);

    @Insert("INSERT INTO FLIGHT_IRREGULARITY(" +
            "FLIGHT_ID, ABNORMAL_STATUS, ABNORMAL_REASON, " +
            "INTERNAL_STATUS, INTERNAL_REASON, MANUAL_ABNORMAL_STATUS, MANUAL_ABNORMAL_REASON, " +
            "MANUAL_INTERNAL_STATUS, MANUAL_INTERNAL_REASON) " +
            "VALUES(" +
            "#{dflt.flightId,jdbcType=NUMERIC}, " +
            "'HBTT', " +
            "NULL, " +
            "'HBTT', " +
            "NULL, " +
            "NULL, NULL, NULL, NULL)")
    int insertIrregularity(DfmeHbttEntity entity);
}